# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit eutils flag-o-matic

DESCRIPTION="Multi-platform & multi-targets cpu emulator and dynamic translator"
HOMEPAGE="http://fabrice.bellard.free.fr/qemu/"
SRC_URI="http://fabrice.bellard.free.fr/qemu/${P/-user/}.tar.gz"

LICENSE="GPL-2 LGPL-2.1"
SLOT="0"
KEYWORDS="-alpha ~amd64 ~ppc -sparc ~x86"
IUSE="qop"  #qvm86 debug nptl qemu-fast nptlonly"
RESTRICT="strip test"

DEPEND="virtual/libc
	app-text/texi2html
	!<=app-emulation/qemu-0.7.0"
RDEPEND=""

S="${WORKDIR}/${P/-user/}"

set_target_list() {
#	TARGET_LIST="arm-user armeb-user i386-user ppc-user mips-user"
	TARGET_LIST="i386-user ppc-user"
	export TARGET_LIST
}

pkg_setup() {
	if [ "$(gcc-major-version)" == "4" ] && ! use qop; then
		eerror "Qemu must be built with GCC 3, unless"
		eerror "you wish to use the highly experimental"
		eerror "qOP backend."
		die "Qemu must build with GCC 3"
	elif use qop; then
		ewarn "The qOP backend is under heavy development"
		ewarn "only you are responsible if it breaks your"
		ewarn "system. YOU HAVE BEEN WARNED!!"
	fi
}

#RUNTIME_PATH="/emul/gnemul/"
src_unpack() {
	unpack ${A}

	cd "${S}"

	if use qop; then
		epatch "${FILESDIR}/qemu-${PV}-svn-qop.patch"
#		einfo "Applying qemu-0.8.2-svn-qop.patch"
	else
		epatch "${FILESDIR}/qemu-0.7.0-ppc-linker.patch"
		epatch "${FILESDIR}/qemu-0.8.0-gcc4-hacks.patch"
		epatch "${FILESDIR}/qemu-0.8.0-stwbrx.patch"
		epatch "${FILESDIR}/qemu-user-0.7.0-errno.patch"
	fi

	# Alter target makefiles to accept CFLAGS set via flag-o.
	sed -i 's/^\(C\|OP_C\|HELPER_C\)FLAGS=/\1FLAGS+=/' \
		Makefile Makefile.target tests/Makefile
	# Ensure mprotect restrictions are relaxed for emulator binaries
	[[ -x /sbin/paxctl ]] && \
		sed -i 's/^VL_LDFLAGS=$/VL_LDFLAGS=-Wl,-z,execheap/' \
			Makefile.target
	# Prevent install of kernel module by qemu's makefile
	sed -i 's/\(.\/install.sh\)/#\1/' Makefile
}

src_compile() {
	#Let the application set its cflags
	unset CFLAGS

	# Switch off hardened tech
	filter-flags -fpie -fstack-protector

	myconf="--disable-gcc-check"
	set_target_list
#		--interp-prefix=${RUNTIME_PATH}/qemu-%M
	./configure \
		--prefix=/usr \
		--target-list="${TARGET_LIST}" \
		--enable-slirp \
		--kernel-path=${KV_DIR} \
		${myconf} \
		|| die "could not configure"

	emake || die "make failed"
}

src_install() {
	make install \
		prefix=${D}/usr \
		bindir=${D}/usr/bin \
		datadir=${D}/usr/share/qemu \
		docdir=${D}/usr/share/doc/${P} \
		mandir=${D}/usr/share/man || die

	rm -fR "${D}/usr/share/{man,qemu}"
	rm -fR "${D}/usr/bin/qemu-img"
}
